/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.extension.ExtensionUtil;
/*     */ import com.zimbra.cs.servlet.ZimbraServlet;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.OutputStream;
/*     */ import java.lang.reflect.Method;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class AdminFileDownload
/*     */   extends ZimbraServlet
/*     */ {
/*     */   private static final String ACTION_GETBP_RESULTS = "getBP";
/*     */   private static final String ACTION_GETSR = "getSR";
/*     */   private static final String ACTION_GETBP_FILE = "getBulkFile";
/*     */   private static final String fileFormat = "fileFormat";
/*     */   private static final String fileID = "fileID";
/*     */   public static final String FILE_FORMAT_MIGRATION_XML = "migrationxml";
/*     */   public static final String FILE_FORMAT_BULK_XML = "bulkxml";
/*     */   public static final String FILE_FORMAT_BULK_CSV = "csv";
/*     */   public static final String FILE_FORMAT_BULK_IMPORT_ERRORS = "errorscsv";
/*     */   public static final String FILE_FORMAT_BULK_IMPORT_REPORT = "reportcsv";
/*     */   
/*     */   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
/*     */     throws ServletException, IOException
/*     */   {
/*     */     try
/*     */     {
/*  72 */       AuthToken authToken = getAdminAuthTokenFromCookie(req, resp);
/*     */       
/*  74 */       if (authToken == null)
/*  75 */         return;
/*  76 */       String action = req.getParameter("action");
/*  77 */       if ((action != null) && (action.length() > 0)) {
/*  78 */         ZimbraLog.webclient.debug("Receiving the file download request " + action);
/*     */       } else {
/*  80 */         return;
/*     */       }
/*     */       
/*  83 */       if (action.equalsIgnoreCase("getBP")) {
/*  84 */         String aid = req.getParameter("aid");
/*  85 */         String filename = "bp_result.csv";
/*     */         
/*  87 */         if (aid == null) {
/*  88 */           ZimbraLog.webclient.error("Missing required parameter aid ");
/*  89 */           return;
/*     */         }
/*     */         
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  99 */         ZimbraLog.webclient.debug("Download the bulk provision status for uploaded file " + aid);
/*     */         
/*     */ 
/* 102 */         resp.setHeader("Expires", "Tue, 24 Jan 2000 20:46:50 GMT");
/*     */         
/* 104 */         resp.setStatus(200);
/* 105 */         resp.setContentType("application/x-download");
/* 106 */         resp.setHeader("Content-Disposition", "attachment; filename=" + filename);
/* 107 */         writeBulkProvisionResults(resp.getOutputStream(), aid);
/*     */         
/* 109 */         return; }
/* 110 */       if (action.equalsIgnoreCase("getSR"))
/*     */       {
/*     */ 
/*     */ 
/* 114 */         String filename = "search_result.csv";
/* 115 */         String query = req.getParameter("q");
/* 116 */         String domain = req.getParameter("domain");
/* 117 */         String types = req.getParameter("types");
/*     */         
/* 119 */         resp.setHeader("Expires", "Tue, 24 Jan 2000 20:46:50 GMT");
/*     */         
/* 121 */         resp.setStatus(200);
/* 122 */         resp.setContentType("application/x-download");
/*     */         
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 128 */         resp.setHeader("Content-Disposition", "attachment; filename=" + filename);
/* 129 */         writeSearchResults(resp.getOutputStream(), query, domain, types, authToken);
/* 130 */       } else if (action.equalsIgnoreCase("getBulkFile")) {
/* 131 */         String pFileId = req.getParameter("fileID");
/* 132 */         String pFileFormat = req.getParameter("fileFormat");
/* 133 */         String bulkFileName = null;
/* 134 */         String clientFileName = null;
/* 135 */         if (pFileFormat.equalsIgnoreCase("csv")) {
/* 136 */           bulkFileName = String.format("%s%s_bulk_%s_%s.csv", new Object[] { LC.zimbra_tmp_directory.value(), File.separator, authToken.getAccountId(), pFileId });
/* 137 */           clientFileName = "bulk_provision.csv";
/* 138 */         } else if (pFileFormat.equalsIgnoreCase("bulkxml")) {
/* 139 */           bulkFileName = String.format("%s%s_bulk_%s_%s.xml", new Object[] { LC.zimbra_tmp_directory.value(), File.separator, authToken.getAccountId(), pFileId });
/* 140 */           clientFileName = "bulk_provision.xml";
/* 141 */         } else if (pFileFormat.equalsIgnoreCase("migrationxml")) {
/* 142 */           bulkFileName = String.format("%s%s_migration_%s_%s.xml", new Object[] { LC.zimbra_tmp_directory.value(), File.separator, authToken.getAccountId(), pFileId });
/* 143 */           clientFileName = "bulk_provision.xml";
/* 144 */         } else if (pFileFormat.equalsIgnoreCase("errorscsv")) {
/* 145 */           bulkFileName = String.format("%s%s_bulk_errors_%s_%s.csv", new Object[] { LC.zimbra_tmp_directory.value(), File.separator, authToken.getAccountId(), pFileId });
/* 146 */           clientFileName = "failed_accounts.csv";
/* 147 */         } else if (pFileFormat.equalsIgnoreCase("reportcsv")) {
/* 148 */           bulkFileName = String.format("%s%s_bulk_report_%s_%s.csv", new Object[] { LC.zimbra_tmp_directory.value(), File.separator, authToken.getAccountId(), pFileId });
/* 149 */           clientFileName = "accounts_report.csv";
/*     */         }
/* 151 */         if (bulkFileName != null) {
/* 152 */           InputStream is = null;
/*     */           try {
/* 154 */             is = new FileInputStream(bulkFileName);
/*     */           } catch (FileNotFoundException ex) {
/* 156 */             if (is != null) {
/* 157 */               is.close();
/*     */             }
/* 159 */             resp.sendError(404, "File not found");
/* 160 */             return;
/*     */           }
/* 162 */           resp.setHeader("Expires", "Tue, 24 Jan 2000 20:46:50 GMT");
/* 163 */           resp.setStatus(200);
/* 164 */           resp.setContentType("application/x-download");
/* 165 */           resp.setHeader("Content-Disposition", "attachment; filename=" + clientFileName);
/*     */           try {
/* 167 */             ByteUtil.copy(is, true, resp.getOutputStream(), false);
/*     */           } catch (Exception e) {
/* 169 */             ZimbraLog.webclient.error(e);
/*     */           }
/*     */           try {
/* 172 */             is.close();
/* 173 */             File file = new File(bulkFileName);
/* 174 */             file.delete();
/*     */           } catch (Exception e) {
/* 176 */             ZimbraLog.webclient.error(e);
/*     */           }
/*     */         }
/*     */       }
/*     */     } catch (Exception e) {
/* 181 */       ZimbraLog.webclient.error(e);
/* 182 */       return;
/*     */     }
/*     */   }
/*     */   
/*     */   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
/*     */     throws ServletException, IOException
/*     */   {
/* 189 */     doGet(req, resp);
/*     */   }
/*     */   
/*     */   private static void writeBulkProvisionResults(OutputStream out, String aid) throws IOException
/*     */   {
/* 194 */     InputStream in = null;
/* 195 */     StringBuffer sb = new StringBuffer();
/*     */     try
/*     */     {
/* 198 */       Class c = ExtensionUtil.findClass("com.zimbra.bp.BulkProvisionStatus");
/* 199 */       Class[] params = new Class[2];
/* 200 */       params[0] = Class.forName("java.io.OutputStream");
/* 201 */       params[1] = Class.forName("java.lang.String");
/* 202 */       Method m = c.getMethod("writeBpStatusOutputStream", params);
/* 203 */       Object[] paramValue = new Object[2];
/* 204 */       paramValue[0] = out;
/* 205 */       paramValue[1] = aid;
/* 206 */       m.invoke(c, paramValue);
/*     */     } catch (Exception e) {
/* 208 */       ZimbraLog.webclient.error(e);
/*     */     } finally {
/* 210 */       if (in != null) in.close();
/*     */     }
/*     */   }
/*     */   
/*     */   private static void writeSearchResults(OutputStream out, String query, String domain, String types, AuthToken authToken)
/*     */     throws IOException
/*     */   {
/* 217 */     InputStream in = null;
/* 218 */     StringBuffer sb = new StringBuffer();
/*     */     try {
/* 220 */       Class c = ExtensionUtil.findClass("com.zimbra.bp.SearchResults");
/* 221 */       Class[] params = new Class[5];
/* 222 */       params[0] = Class.forName("java.io.OutputStream");
/* 223 */       params[1] = Class.forName("java.lang.String");
/* 224 */       params[2] = Class.forName("java.lang.String");
/* 225 */       params[3] = Class.forName("java.lang.String");
/* 226 */       params[4] = Class.forName("com.zimbra.cs.account.AuthToken");
/* 227 */       Method m = c.getMethod("writeSearchResultOutputStream", params);
/* 228 */       Object[] paramValue = new Object[5];
/* 229 */       paramValue[0] = out;
/* 230 */       paramValue[1] = query;
/* 231 */       paramValue[2] = domain;
/* 232 */       paramValue[3] = types;
/* 233 */       paramValue[4] = authToken;
/* 234 */       m.invoke(c, paramValue);
/*     */     } catch (Exception e) {
/* 236 */       ZimbraLog.webclient.error(e);
/*     */     } finally {
/* 238 */       if (in != null) in.close();
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/AdminFileDownload.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */